﻿using System;
using System.Diagnostics.Contracts;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class DXFN12List
    {
        private DXFN dxfn;
        private XFExtNoFRT? xfext;

        public DXFN12List(BinaryReader reader, int length)
        {
            Read(reader, length);
        }


        private void Read(BinaryReader reader, int length)
        {
            long position = reader.BaseStream.Position;
            dxfn = new DXFN();
            dxfn.Read(reader);
            if (length > reader.BaseStream.Position - position)
            {
                xfext = new XFExtNoFRT();
                xfext.Value.Read(reader);
            }
        }

        public void Write(BinaryWriter writer)
        {
            dxfn.Write(writer);
            if (xfext != null)
                xfext.Value.Write(writer);
        }

        public DifferentialStyleRecord ToFormattingRecord(BiffReaderWorksheetContext context)
        {
            Contract.Ensures(Contract.Result<DifferentialStyleRecord>() != null);
            IPropertyProvider provider = null;
            if (this.xfext != null)
                provider = xfext.Value;
            DifferentialStyleRecord result = dxfn.ToDifferencialStyleRecord(context.ParentContext, provider);
            if (xfext != null)
            {
                XFExtNoFRT value = xfext.Value;
                foreach (ExtProp property in value.ExtProps)
                {
                    switch (property.extType)
                    {
                        case ExtPropType.BackgroundColor:
                        case ExtPropType.ForegroundColor:
                        case ExtPropType.BottomBorderColor:
                        case ExtPropType.CellTextColor:
                        case ExtPropType.DiagonalBorderColor:
                        case ExtPropType.FontScheme:
                        case ExtPropType.GradientFill:
                        case ExtPropType.Indentation:
                        case ExtPropType.LeftBorderColor:
                        case ExtPropType.RightBorderColor:
                        case ExtPropType.TopBorderColor:
                            throw new NotImplementedException();
                        default:
                            throw new NotSupportedException();
                    }
                }
            }
            return result;
        }
    }
}